package Algorithms.chapter1_3;

import Algorithms.StdIn;
import Algorithms.StdOut;

public class FixedCapacityStackOfStrings {
    //格式指定为字符串
    public static class FixedCapacityStackOfStrings2 {
        private String[] a;
        private int N;
        //创建对象

        public FixedCapacityStackOfStrings2(int cap) {
            a = new String[cap];
        }

        //是否为空
        public boolean isEmpty() {
            return N == 0;
        }

        //长度
        public int size() {
            return N;
        }

        //push
        public void push(String item) {
            a[N++] = item;
        }

        //pop
        public String pop() {
            return a[--N];
        }
    }

    //泛型
    public static class FixedCapacityStack<item> {
        private item[] a;
        private int N;

        //创建对象
        public FixedCapacityStack(int cap) {
            a = (item[]) new Object[cap]; //特殊的对象创建方式
        }

        //是否为空
        public boolean isEmpty() {
            return N == 0;
        }

        //长度
        public int size() {
            return N;
        }

        //push
        public void push(item item) {
            a[N++] = item;
        }

        //pop
        public item pop() {
            return a[--N];
        }
    }


//    public static void main(String[] args) {
//        FixedCapacityStackOfStrings2 s;
//        s = new FixedCapacityStackOfStrings2(100);
//        while (!StdIn.isEmpty()) {
//            String item = StdIn.readString();
//            if (!item.equals("-"))
//                s.push(item);
//            else if (!s.isEmpty()) StdOut.print(s.pop() + " ");
//        }
//        StdOut.println("(" + s.size() + " left on stack");
//
//    }

    public static void main(String[] args) {
        FixedCapacityStack<String> s;
        s = new FixedCapacityStack<String>(100);
        while (!StdIn.isEmpty()) {
            String item = StdIn.readString();
            if (!item.equals("-"))
                s.push(item);
            else if (!s.isEmpty()) StdOut.print(s.pop() + " ");
        }
        StdOut.println("(" + s.size() + " left on stack");

    }



}
